package com.monkeyboy.rabbitmq.routing;

import com.monkeyboy.rabbitmq.RabbitmqConnectionUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * 作用： 生产者生产消息
 * 
 * @author Gavin
 * @time:2018年10月24日
 */
public class Producer {
	private final static String EXCHANGE_NAME = "test_exchange_direct";

	public static void main(String[] args) throws Exception {
		// 获取到连接一级mq通道(相当于mysql获取连接)
		Connection connection = RabbitmqConnectionUtil.getConnection();
		// 从连接中创建通道(相当于mysql获取连接后还得获取到statement才能操作数据库一样的道理)
		Channel channel = connection.createChannel(1);

		// 声明exchange
		channel.exchangeDeclare(EXCHANGE_NAME, "direct");
		// 消息内容
		String message = "商品已经更新了，id=123456";
		// 发送消息到交换机里面
		String routeKey = "update";// 指定路由key值
		channel.basicPublish(EXCHANGE_NAME, routeKey, null, message.getBytes());
		System.out.println("后台系统send--商品已更新--->" + message);
		// 关闭通道和连接
		channel.close();
		connection.close();
	}
}
